package js.dwim.domain;

import java.io.Serializable;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import js.dwim.server.Persistence;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable( detachable = "true" )
public class MapNode
        implements Serializable
{
    private static final long serialVersionUID = 1L;

    @PrimaryKey
    @Persistent( valueStrategy = IdGeneratorStrategy.IDENTITY )
    private Key key;

    @Persistent
    public final static int ACTIVE_RADIUS = 30;

    @Persistent( defaultFetchGroup = "true", dependent = "true" )
    @Embedded
    private Point2D location;

    @Persistent
    private Key player;
    
    @Persistent
    private WorldMap world;

    public MapNode( Point2D location, Player player, WorldMap world )
    {
        this.location   = location;
        this.player     = player.getKey();
        this.world      = world;
    }

    public boolean interactsWith( MapNode target )
    {
        return this.location.distance( target.location ) < ACTIVE_RADIUS;
    }

    public void setPlayer( Player player )
    {
        this.player = player.getKey();
    }

    public Player getPlayer()
    {
        return Persistence.getObjectByKey( Player.class, player );
    }

    public Key getKey()
    {
        return key;
    }

    public Point2D getLocation()
    {
        return location;
    }

    public void setWorld( WorldMap world )
    {
        this.world = world;
    }

    public WorldMap getWorld()
    {
        return world;
    }
}
